package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.jnzc.ocr.common.utils.ArrayHandleUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.serv.shop.entity.ShopProductAttr;
import com.jnzc.ocr.serv.shop.entity.ShopProductSku;
import com.jnzc.ocr.serv.shop.entity.ShopProductSkuAttr;
import com.jnzc.ocr.serv.shop.filter.ShopProductSkuAttrFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopProductSkuAttrMapper;
import com.jnzc.ocr.serv.shop.service.ShopProductAttrService;
import com.jnzc.ocr.serv.shop.service.ShopProductSkuAttrService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017-12-24
 */
@Service
public class ShopProductSkuAttrServiceImpl extends MybatisServiceImpl<ShopProductSkuAttrMapper, ShopProductSkuAttr, ShopProductSkuAttrFilter> implements ShopProductSkuAttrService {

    @Autowired
    private ShopProductAttrService shopProductAttrService;

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopProductSkuAttr> ew, ShopProductSkuAttrFilter filter) {
        ShopProductSkuAttr entity = new ShopProductSkuAttr();
        entity.setShopId(filter.getShopId());
        entity.setProductId(filter.getProductId());
        entity.setSkuId(filter.getSkuId());
        ew.setEntity(entity);
        if(CollectionUtils.isNotEmpty(filter.getSkuIdList())) {
            ew.in("sku_id", filter.getSkuIdList());
        }
    }

    @Override
    public void save(ShopProductSku sku, List<Long> attrIdList) {
        this.deleteBySkuId(sku.getId(), sku.getShopId());
        if(CollectionUtils.isEmpty(attrIdList)) {
            return;
        }
        List<ShopProductSkuAttr> list = Lists.newArrayList();
        for (Long attrId : attrIdList) {
            ShopProductSkuAttr obj = new ShopProductSkuAttr();
            obj.setShopId(sku.getShopId());
            obj.setProductId(sku.getProductId());
            obj.setSkuId(sku.getId());
            obj.setAttrId(attrId);
            list.add(obj);
        }
        super.insertList(list);
    }

    @Override
    public void deleteByProductId(Long productId, Long shopId) {
        ValidateUtils.isId(productId, "产品ID");
        ShopProductSkuAttrFilter filter = new ShopProductSkuAttrFilter();
        filter.setProductId(productId);
        filter.setShopId(shopId);
        super.delete(filter);
    }

    @Override
    public void deleteBySkuId(Long skuId, Long shopId) {
        ValidateUtils.isId(skuId, "SKU的ID");
        this.deleteBySkuId(Lists.newArrayList(skuId), shopId);
    }

    @Override
    public void deleteBySkuId(List<Long> skuIdList, Long shopId) {
        if(CollectionUtils.isEmpty(skuIdList)) {
            return;
        }
        ShopProductSkuAttrFilter filter = new ShopProductSkuAttrFilter();
        filter.setSkuIdList(skuIdList);
        filter.setShopId(shopId);
        super.delete(filter);
    }

    @Override
    public Map<Long, List<ShopProductSkuAttr>> listSkuMapByProductId(Long productId, Long shopId) {
        ValidateUtils.isId(productId, "产品ID");
        ShopProductSkuAttrFilter filter = new ShopProductSkuAttrFilter();
        filter.setProductId(productId);
        filter.setShopId(shopId);
        List<ShopProductSkuAttr> list = super.list(filter);
        return ArrayHandleUtils.parseMapGroup(list, "skuId");
    }
}
